package com.newhero.app.report.web.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.newhero.product.common.controller.BaseController;
import com.newhero.product.common.mybatis.mapper.EntityWrapper;
import com.newhero.product.common.util.StringUtils;
import com.newhero.product.common.vo.Json;
import com.newhero.product.common.mybatis.plugins.Page;
import com.newhero.app.report.persistence.entity.ProblemQuality;
import com.newhero.app.report.service.IProblemQualityService;
import com.newhero.product.util.ExportUtil;

 /**
 * @ClassName: ProblemQualityController
 * @Description: 访问控制类
 * @author zlf
 * @date 2019-01-14
 */
@Controller
@RequestMapping("problemQuality")
public class ProblemQualityController extends BaseController {
    
    @Autowired 
    private IProblemQualityService problemQualityServiceImpl;
    												
    /**
     * 列表页面
     * @return
     */    
	@RequestMapping(value = "/problemQualityList", method ={RequestMethod.GET,RequestMethod.POST })
	public String problemQualityList(
			@RequestParam(value = "page", defaultValue = "1", required = true) int page,// 页号
			@RequestParam(value = "rows", defaultValue = "" + _ROWS_PER_PAGE, required = true) int rows,// 每页显示的记录条数
			@RequestParam(value = "orderField", defaultValue = "problem_no", required = true) String orderField,
			@RequestParam(value = "isAsc", defaultValue = "0", required = true) int isAsc,//1-代表true;0-代表false
			ProblemQuality problemQuality,
			HttpServletRequest request, HttpServletResponse response, ModelMap model) {
		Page<ProblemQuality> p = new Page<ProblemQuality>();
		p.setCurrent(page);
		p.setSize(rows);
		p.setOrderByField(orderField);
		if(isAsc==0){
			p.setAsc(false);
		}else{
			p.setAsc(true);
		}
		
		EntityWrapper<ProblemQuality> entityWrapper = new EntityWrapper<ProblemQuality>();
		
		
    	if(StringUtils.isNotBlank(problemQuality.getPid())){
			entityWrapper.like("pid", problemQuality.getPid());
		}
		model.addAttribute("pid", problemQuality.getPid());
		
			
    	if(StringUtils.isNotBlank(problemQuality.getProblemName())){
			entityWrapper.like("problem_name", problemQuality.getProblemName());
		}
		model.addAttribute("problemName", problemQuality.getProblemName());
		
				
    	if(StringUtils.isNotBlank(problemQuality.getProblemNo())){
			entityWrapper.like("problem_no", problemQuality.getProblemNo());
		}
		model.addAttribute("problemNo", problemQuality.getProblemNo());
		
	
		p = problemQualityServiceImpl.selectPage(p, entityWrapper);
		model.addAttribute("data", p);
		model.addAttribute("orderField", orderField);
		model.addAttribute("isAsc", isAsc);
		
		return "/report/problemQuality/problemQualityList";
	}
	
    /**
     * 表单页面
     * @return
     */
	@RequestMapping(value = "/problemQualityForm", method = RequestMethod.GET)
	public String problemQualityForm(String pid, HttpServletRequest request) {
		if (pid != null && !"".equals(pid)) {
			request.setAttribute("pid", pid);
			request.setAttribute("formData",problemQualityServiceImpl.selectById(pid));
		} else {
			request.setAttribute("pid", "");
			request.setAttribute("formData", new HashMap<String, Object>());
		}
		return "/report/problemQuality/problemQualityForm";
	}
    
    /**
     * 表单保存
     * @return
     */
	@RequestMapping(value = "/problemQualitySave", method = RequestMethod.POST)
	public String problemQualitySave(
			String pid, ProblemQuality problemQuality, 
			HttpServletRequest request,HttpServletResponse response, ModelMap model) {
		try {
			if (!StringUtils.isBlank(pid)) {
				model.addAttribute("flag", "数据修改成功！");
				problemQualityServiceImpl.updateById(problemQuality);
			} else {
				model.addAttribute("flag", "数据添加成功！");
				problemQualityServiceImpl.insert(problemQuality);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
		}
		return "redirect:problemQualityList";
	}

    /**
     * 删除
     * @param ids
     * @return
     */
	@RequestMapping(value = "/problemQualityDelete", method = { RequestMethod.POST, RequestMethod.GET })
	public String problemQualityDelete(
			@RequestParam(value = "ids", required = true) String ids,
			HttpServletRequest request, HttpServletResponse response, ModelMap model) {
		String[] idArr = ids.split(",");
		problemQualityServiceImpl.deleteBatchIds(Arrays.asList(idArr));
		model.addAttribute("flag", "数据删除成功！");
		return "redirect:problemQualityList";
	}     
    
    
    @ResponseBody
	@RequestMapping(value = "/problemQualityAjax", method = RequestMethod.POST)
	public Json problemQualityAjax(String $primaryKey) {
		ProblemQuality problemQuality = problemQualityServiceImpl.selectById($primaryKey);
		Json json = new Json();
		json.setSuccess(true);
		json.setObj(problemQuality);
		return json;
	}
	
	@RequestMapping(value = "/problemQualityExportExcel", method = { RequestMethod.GET, RequestMethod.POST })
	public void problemQualityExportExcel(
			ProblemQuality problemQuality,
			HttpServletRequest request, HttpServletResponse response, ModelMap model) {
		
		EntityWrapper<ProblemQuality> entityWrapper = new EntityWrapper<ProblemQuality>();
		
						
		List<ProblemQuality> resultList = problemQualityServiceImpl.selectList(entityWrapper);
		
		ExportUtil.export(request, response, resultList,"导出Excel","problem_quality", 0, 2, 0, 16);
	}
}
